package cj.web.prize.controllers;

import java.util.List;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.web.constants.MemuEnum;
import cj.web.prize.dao.PrizeGiftDAO;
import cj.web.prize.domain.PrizeGift;

@Path("/activeGiftProbability")
public class ActiveGiftProbabilityController{
	private static Logger logger = Logger.getLogger(ActiveGiftProbabilityController.class.getName());
	
	@Autowired
	private PrizeGiftDAO prizeGiftDao;
	
	@Get("show")
	public String show(Model model) {
		//MemuEnum
		model.add("MemuEnum",MemuEnum.jpglzjgl);
		return "/views/prize/gpList.vm";
	}

	@Post("getList")
	public String queryList(PrizeGift prizeGift) {
	
		List<PrizeGift> arr = prizeGiftDao.query(prizeGift);
		
		JSONObject jo = new JSONObject();

		int size = prizeGiftDao.querySize(prizeGift);

		jo.put("data", arr);
		jo.put("draw", prizeGift.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@"+jo.toString();	
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id)
	{
		PrizeGift obj = prizeGiftDao.queryById(id);
		
		if(obj!=null){
			model.add("obj",obj);
		}
		//MemuEnum
		model.add("MemuEnum",MemuEnum.jpglzjgl);
		return "/views/prize/gpEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,PrizeGift prizeGift)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			prizeGiftDao.update(prizeGift);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
}